import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
Vue.use(Vuex)

export default new Vuex.Store({
  state:{
      list:[],
      status:'all'
  },
  mutations:{
    SET_LIST(state,data){
      state.list=data;
    },
    ADD_LIST(state,data){
      state.list.push(data)
    },
    CHANGE_TYPE(state,index){
      state.list[index].finished=!state.list[index].finished;

      localStorage.setItem('list',JSON.stringify(state.list))
    },
    REMOVE_LIST(state,index){
      state.list.splice(index,1);
    },
    CHANNGE_STATUS(state,status){
      state.status=status;
    },
    REMOVE_COMPLETE(state){
      state.list=state.list.filter(item=>!item.finished)
    },
    SET_TOTAL_LIST(state,boo){
      if(boo){
        state.list.map(item=>item.finished=true)
      }else{
        state.list.map(item=>item.finished=false)
      }
    }
  },
  actions:{
    setList({commit}){
      const list=JSON.parse(localStorage.getItem('list'))||[];
      commit('SET_LIST',list)
    },
    addList({commit},data){
      commit('ADD_LIST',data)
    },
    changeType({commit},index){
      commit('CHANGE_TYPE',index)
    },
    removeList({commit},index){
      commit('REMOVE_LIST',index)
    },
    changeStatus({commit},status){
      commit('CHANNGE_STATUS',status)
    },
    removeComplete({commit}){
      commit('REMOVE_COMPLETE')
    },
    setTotalList({commit},boo){
      commit('SET_TOTAL_LIST',boo)
    }
  }
})